

import '../struct/message.dart';
import '../struct/replyCode.dart';
import '../struct/request_id.dart';
import 'frame.dart';

class Parse {
  static Frame? isCodeOK(List<int> reply) {
    if (reply.length < 4) {
      return null;
    }
    final frame = Frame(reply);
    final code = frame.popByte();
    if (code == ReturnCode.REPLY_SUCCESS.index) {
      if (!frame.verify()) {
        return null;
      }
      if (frame.popShort() != frame.length - 1) {
        print("长度不对,${frame.length}");
        return null;
      }
      return frame;
    }
    print("返回值不对:$code");
    return null;
  }

  static RequestBean generateRequest(RequestId requestId, List<int> data) {
    return RequestBean(2, requestId, data.length, data);
  }

  static MessageBean parseMessage(Frame frame) {
    if (frame.length <= 1) {
      return MessageBean(MessageId.MSG_NONE, Frame(null));
    }
    final messageId = frame.popByte();
    if (MessageId.values.length <= messageId) {
      return MessageBean(MessageId.MSG_NONE, Frame(null));
    }

    return MessageBean(MessageId.values[messageId], frame);
  }
}
